package com.gree.scmservice.controller.saas.tenant;

import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.TablePageData;
import com.gree.framework.entity.SaasTenant;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.entity.saas.vo.SaasTenantPasswordVo;
import com.gree.scmservice.entity.saas.vo.SaasTenantVo;
import com.gree.scmservice.service.saas.SaasTenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 接口管理
 * Created by 180686 on 2021/02/17 14:48
 */

@RestController
@Api(tags = "租户信息(租户端)")
@RequestMapping("/saas/tenant")
public class SaasTenantController {

    @Autowired
    private SaasTenantService saasTenantService;

    /**
     * saas系统注册用户
     * @param saasTenantVo
     * @return
     */
    @PostMapping("/register")
    public Object register(@Validated @RequestBody SaasTenantVo saasTenantVo){
        return saasTenantService.register(saasTenantVo);
    }

    /**
     * 修改密码(管理员)
     * @return
     */
    @PutMapping("/admin/reset")
    @ApiOperation(value = "修改密码(管理员)")
    @ApiImplicitParam(name = "saasTenantPasswordVo", value = "密码信息", dataType = "SaasTenantPasswordVo")
    public ReturnVo updatePasswordById(@Validated @RequestBody SaasTenantPasswordVo saasTenantPasswordVo)
    {
        return ReturnVo.toAjax(saasTenantService.resetPasswordOnUser(saasTenantPasswordVo));
    }

    /**
     * 修改密码(租户)
     * @return
     */
    @PutMapping("/tenant/reset")
    @ApiOperation(value = "修改密码(租户)")
    @ApiImplicitParam(name = "saasTenantPasswordVo", value = "密码信息", dataType = "SaasTenantPasswordVo")
    public ReturnVo updatePasswordOnUser(@Validated @RequestBody SaasTenantPasswordVo saasTenantPasswordVo)
    {
        return ReturnVo.toAjax(saasTenantService.resetPasswordOnUser(saasTenantPasswordVo));
    }

    /**
     * 查询租户信息列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询租户信息列表")
    @ApiImplicitParam(name = "saasSysTenant", value = "租户信息", dataType = "SaasSysTenant")
    public ReturnVo<TablePageData<SaasTenant>> list(SaasTenant saasTenant)
    {
        return ReturnVo.success(saasTenantService.querySaasTenant(saasTenant));
    }

    /**
     * 获取租户信息详细信息
     */
    @GetMapping(value = "/{tenant_id}")
    @ApiOperation(value = "获取租户信息详细信息")
    @ApiImplicitParam(name = "tenantId", value = "租户ID", dataType = "Long")
    public ReturnVo fetchSaasTenant(@PathVariable("tenant_id") String tenantId)
    {
        return ReturnVo.success(saasTenantService.fetchSaasTenant(tenantId));
    }

    /**
     * 租户获取
     * @return
     */
    @GetMapping(value = "/me")
    public ReturnVo getInfo()
    {
        String tenantId = SecurityUtils.getUserWithPasswordMode().get("username").toString();
        return ReturnVo.success(saasTenantService.fetchSaasTenant(tenantId));
    }

    /**
     * 新增租户信息
     * @param saasTenant
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增租户信息")
    @ApiImplicitParam(name = "saasTenant", value = "租户信息", dataType = "SaasTenant")
    public ReturnVo add(@RequestBody SaasTenant saasTenant)
    {
        return ReturnVo.toAjax(saasTenantService.addSaasTenant(saasTenant));
    }

    /**
     * 更新租户信息
     * @param saasTenant
     * @return
     */
    @PutMapping
    @ApiOperation(value = "更新租户信息")
    @ApiImplicitParam(name = "saasTenant", value = "租户信息", dataType = "SaasTenant")
    public ReturnVo edit(@RequestBody SaasTenant saasTenant)
    {
        return ReturnVo.toAjax(saasTenantService.updateSaasTenant(saasTenant));
    }

    /**
     * 删除租户信息
     * @param tenantIds
     * @return
     */
    @DeleteMapping("/{tenant_ids}")
    @ApiOperation(value = "删除租户信息")
    @ApiImplicitParam(name = "tenantIds", value = "租户信息", dataType = "Long[]")
    public ReturnVo remove(@PathVariable("tenant_ids") Long[] tenantIds)
    {
        return ReturnVo.toAjax(saasTenantService.delSaasTenant(tenantIds));
    }


}
